package ua.kharkiv.knure.karimov.Task1;

/**
 * Very-very long (and slow) int
 */
public class VeryLongInt {
	private String number;

	public VeryLongInt() {
		number = "0";
	}

	public String Sub(VeryLongInt i) {
		return Sub(number + "", i.getNumber() + "");
	}

	public VeryLongInt(String number) {
		this.number = number;
	}

	public String getNumber() {
		return number + "";
	}

	public void setNumber(String number) {
		this.number = number + "";
	}

	public static String Sum(String num1, String num2) {
		VeryLongInt test = new VeryLongInt();
		test.number = num1 + "";
		return test.Sum(num2 + "");
	}

	public String Sum(String num2) {
		String number = this.number + "";
		if (number.length() > 1 && number.substring(0, 1).equals("-")) {
			number = number.substring(1);
			return Sub(num2, number);
		}
		if (num2.length() > 1 && num2.substring(0, 1).equals("-")) {
			num2 = num2.substring(1);
			return Sub(number, num2);
		}
		if (number.length() < num2.length())
			while (number.length() < num2.length())
				number = "0" + number;
		if (number.length() > num2.length())
			while (number.length() > num2.length())
				num2 = "0" + num2;
		Integer n1, n2;
		String result = "";

		while (number.length() > 0 && num2.length() > 0) {
			if (number.length() > 0)
				n1 = new Integer(number.substring(0, 1));
			else
				n1 = new Integer(0);

			if (num2.length() > 0)
				n2 = new Integer(num2.substring(0, 1));
			else
				n2 = new Integer(0);

			if (number.length() > 0)
				number = number.substring(1);
			if (num2.length() > 0)
				num2 = num2.substring(1);
			n1 += n2;

			if (n1 < 10)
				result += n1;
			else {
				result += n1 % 10;
				result = Sum(result, n1 / 10 + "0");
			}
		}

		return result;
	}

	public String Mul(String num2) {
		return Mul(number + "", num2 + "");
	}

	public static String Mul(String number, String num2) {
		String result = "";
		Integer zn1 = 1, zn2 = 1;
		if (number.length() > 1 && number.substring(0, 1).equals("-")) {
			zn1 = -1;
			number = number.substring(1);
		}
		if (num2.length() > 1 && num2.substring(0, 1).equals("-")) {
			zn2 = -1;
			num2 = num2.substring(1);
		}

		for (int i = 0; i < num2.length(); i++) {
			result += "0";
			String r2 = "";
			Integer n2 = new Integer(num2.substring(i, i + 1));
			for (int j = 0; j < number.length(); j++) {
				Integer n1 = new Integer(number.substring(j, j + 1));
				n1 *= n2;
				if (n1 < 10)
					r2 += n1;
				else {
					r2 += n1 % 10;
					r2 = Sum(r2, n1 / 10 + "0");
				}
			}
			result = Sum(result, r2);
		}
		zn1 *= zn2;
		if (zn1 == -1)
			return zn1.toString().substring(0, 1) + result;
		return result;
	}

	public int compare(VeryLongInt n) {
		return compare(n.getNumber());
	}

	public int compare(String num2) {
		if (number.length() < num2.length())
			return -1;
		if (number.length() > num2.length())
			return 1;
		for (int i = 0; i < number.length(); i++)
			if (number.charAt(i) > num2.charAt(i))
				return 1;
			else if (number.charAt(i) < num2.charAt(i))
				return -1;
		return 0;
	}

	public String Mul(VeryLongInt num2) {
		return num2.Mul(number + "");
	}

	public String Sum(VeryLongInt num2) {
		return Sum(number + "", num2.getNumber() + "");
	}

	public static String Sub(String num1, String num2) {
		VeryLongInt test = new VeryLongInt();
		test.number = num1 + "";
		return test.Sub(num2 + "");
	}

	public String Sub(String num2) {
		String number = this.number + "";
		Integer zn1 = 1, zn2 = -1;
		if (number.length() > 1 && number.substring(0, 1).equals("-")) {
			number = number.substring(1);
			zn1 = zn1 * -1;
		}
		if (num2.length() > 1 && num2.substring(0, 1).equals("-")) {
			num2 = num2.substring(1);
			zn2 = zn2 * -1;
		}

		if (zn1 == zn2) {
			if (zn1 == -1)
				return "-" + Sum(num2);
			return Sum(num2);

		}

		if (zn1 < 0 && zn2 > 0)
			return Sub(num2, number);

		if (compare(num2) < 0)
			return Mul("-1", Sub(num2, number));

		if (number.length() > num2.length())
			while (number.length() > num2.length())
				num2 = "0" + num2;

		Integer n1, n2;
		String result = "";
		for (int i = 0; i < number.length(); i++) {
			if (number.length() > 0)
				n1 = new Integer(number.substring(i, i + 1));
			else
				n1 = new Integer(0);

			if (num2.length() > 0)
				n2 = new Integer(num2.substring(i, i + 1));
			else
				n2 = new Integer(0);
			if (n1 < n2) {
				n1 = n1 + 10 - n2;
				result = Sub(result + n1, "10");
			} else {
				n1 -= n2;
				result += n1;
			}

		}

		if (result.length() > 1)
			while (result.charAt(0) == '0')
				result = result.substring(1);
		return result;
	}

	public boolean equals(VeryLongInt n2) {
		return equals(n2.getNumber());
	}

	@Override
	public String toString() {
		return number;
	}

	public boolean equals(String n2) {
		String num2 = n2;
		if (number.length() != num2.length())
			return false;
		for (int i = 0; i < number.length(); i++)
			if (number.charAt(i) != num2.charAt(i))
				return false;
		return true;
	}
}
